package com.jfl.service.impl.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfl.base.BaseServiceImpl;
import com.jfl.common.Constants;
import com.jfl.common.MessageKey;
import com.jfl.config.Resources;
import com.jfl.exception.BusinessException;
import com.jfl.mapper.SysDeptMapper;
import com.jfl.model.SysDept;
import com.jfl.system.SysDeptService;

/**
 * 部门
 *  
 * @author  赵炎
 * @version  [V1.00, 2018年12月11日]
 * @since V1.00
 */
@Component
@Service(version="${jfl.version}")
public class SysDeptServiceImpl extends BaseServiceImpl<SysDept, SysDeptMapper> implements SysDeptService
{

    @Override
    public List<Map<String, Object>> selectDeptTree(SysDept sysDept)
    {
        List<Map<String, Object>> trees = Lists.newArrayList();
        List<SysDept> deptList = this.mapper.selectByMap(Maps.newHashMap());
        trees = getTrees(deptList, false ,null);
        return trees;
    }
    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param isCheck 是否需要选中
     * @param roleDeptList 角色已存在菜单列表
     * @return
     */
    public List<Map<String, Object>> getTrees(List<SysDept> deptList, boolean isCheck, List<String> roleDeptList)
    {

        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (SysDept dept : deptList)
        {
            if (Constants.STATUS_NORMAL.equals(dept.getStatus()))
            {
                Map<String, Object> deptMap = new HashMap<String, Object>();
                deptMap.put("id", dept.getId());
                deptMap.put("pId", dept.getParentId());
                deptMap.put("name", dept.getDeptName());
                deptMap.put("title", dept.getDeptName());
                if (isCheck)
                {
                    deptMap.put("checked", roleDeptList.contains(dept.getId() + dept.getDeptName()));
                }
                else
                {
                    deptMap.put("checked", false);
                }
                trees.add(deptMap);
            }
        }
        return trees;
    }
    @Override
    public String checkDeptNameUnique(SysDept dept)
    {
        Long deptId = dept.getId() == null ? -1L: dept.getId();
        SysDept record = this.mapper.selectDeptByName(dept.getDeptName());
        return result(record, deptId);
    }
    @Transactional
    @Override
    public void deleteByDeptId(Long deptId) throws BusinessException
    {
        int count = this.mapper.selectCountByParentId(deptId);
        if(count > 0)
        {
            throw new BusinessException(Resources.getMessage(MessageKey.DEPT_EXIT_CHILD));
        }
        count = this.mapper.selectCountUserByDeptId(deptId);
        if(count > 0)
        {
            throw new BusinessException(Resources.getMessage(MessageKey.DEPT_HAS_ASSIGNED));
        }
        this.mapper.deleteById(deptId);
    }   
}
